package net.sunzc.hwfacetest;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.Window;
import android.widget.Toast;
import com.hanvon.face.HanvonfaceAddUserView;
import com.hanvon.face.IRegisterCallback;
import com.hanvon.faceRec.FaceCoreHelper;
import com.hanvon.faceRec.HWFaceLib;
import net.sunzc.hwfacetest.utils.ReadFromFile;

@SuppressLint("NewApi")
public class AddFaceDelog extends Activity implements IRegisterCallback {

    private static final String NUM = "id_d";
    private static final String TAG = "AddFaceDelog";
    private static final String FACE_DATA = "face_data";
    /* 设置需要像素的宽*高--后边注释参数为次Camera支持的参数 */
    private static int PIXEL_WIDTH = 1280;// 1280,800,768,720,640,576,480,384,352
    private static int PIXEL_HEIGHT = 720;// 720,480,432,480,480,432,320,288,288
    private int mOpenedCamera = 0;
    private HanvonfaceAddUserView mShowSurfaceView;
    private String mUserName;

    public static void addFace(Activity context, String displayContents, int requestCode) {
        Intent intent = new Intent(context, AddFaceDelog.class);
        intent.putExtra(NUM, displayContents);
        context.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        HWFaceLib.getInstance().InitFaceEngine(FaceCoreHelper.SERIAL_NUMBER);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//
        // 强制竖屏
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        int nCameraNum = Camera.getNumberOfCameras();
        if (nCameraNum == 1) {
            mOpenedCamera = 0;
            Camera.getCameraInfo(mOpenedCamera, cameraInfo); // get camerainfo
        } else {
            mOpenedCamera = CameraInfo.CAMERA_FACING_FRONT;
            Camera.getCameraInfo(mOpenedCamera, cameraInfo); // get camerainfo
        }
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.addface_delog);
        Intent intent = this.getIntent();
        mUserName = intent.getStringExtra(NUM);
        init();
    }

    private void init() {
        // 打开检测窗口
        mShowSurfaceView = (HanvonfaceAddUserView) findViewById(R.id.hanvonfaceAddUserView1);
        mShowSurfaceView.onInitSDK(640, 480, mOpenedCamera, 1, mUserName);
        /* 以SurfaceView作为相机Preview之用mSurfaceView01显示相机mSurfaceView02显示脸框 */
        mShowSurfaceView
                .setSurfaceView((SurfaceView) findViewById(R.id.mSurfaceView01));
        mShowSurfaceView.onRegisterResultCallback(this);
        mShowSurfaceView.bTest = true;
    }


    int a = 0;

    @Override
    public void registerCallback(int nResultType, int mProgressName, String strLog) {
        Log.i(TAG, "nResultType:" + nResultType + "mProgressName:" + mProgressName + "strLog:" + strLog);
        // TODO Auto-generated method stub
        switch (nResultType) {
            case 0:
                a++;
                if (a == 1) {
                    Toast.makeText(this, "正在录制，请等待……", 1000).show();
                }
                break;
            case 1:
                Toast.makeText(this, "采集完毕，等待上传！", 1000).show();
                String mFace = ReadFromFile.readOriginal2Hex(HWFaceLib.getFaceFeatureFile(this));
                Intent intent = new Intent();
                intent.putExtra(FACE_DATA, mFace);
                setResult(RESULT_OK, intent);
                finish();
                break;
        }
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        // 被交换到后台时执行
        // Thread.currentThread().interrupt();
        super.onPause();
        if (mShowSurfaceView != null) {
            mShowSurfaceView.onReleaseSDK();
        }
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        mShowSurfaceView.onStartPreview();
    }


    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        HWFaceLib.clearFaceFeatureFile(this);
    }

    public static String getFaceData(int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            return data.getStringExtra(FACE_DATA);
        }
        return null;
    }
}
